package com.ruibang.glass.produce.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.produce.constant.ProduceErrorCode;
import com.ruibang.glass.produce.domain.req.Cold1DispatchReq;
import com.ruibang.glass.produce.entity.Cold1Dispatch;
import com.ruibang.glass.produce.mapper.Cold1DispatchMapper;
import com.ruibang.glass.produce.service.Cold1DispatchService;
import com.teaming.cloud.framework2.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-06-06  14:23
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class Cold1DispatchServiceImpl extends ServiceImpl<Cold1DispatchMapper, Cold1Dispatch> implements Cold1DispatchService {

    @Autowired
    @Qualifier("produceThreadPool")
    private Executor executor;


    @Override
    public Cold1Dispatch getData(Cold1DispatchReq cold1DispatchReq) {
        //默认查询最近2个小时的数据
        if (cold1DispatchReq.getStartTime() == null || cold1DispatchReq.getEndTime() == null) {
            LocalDateTime endTime = LocalDateTime.now();
            LocalDateTime startTime = endTime.minusHours(2);
            cold1DispatchReq.setStartTime(startTime);
            cold1DispatchReq.setEndTime(endTime);
        }
        //获取最新采集数据
//        LocalDateTime latestTime = baseMapper.getLatestTime();
//        LocalDateTime currentTime = LocalDateTime.now();


        //统计缺陷数据
        CompletableFuture<Cold1Dispatch> completableFuture1 = CompletableFuture.supplyAsync(() -> baseMapper.getData(cold1DispatchReq), executor);


        //统计横切数据
        CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> this.getHqNum(cold1DispatchReq.getStartTime(), cold1DispatchReq.getEndTime()), executor);


        //冷端总投入数
        CompletableFuture<Integer> completableFuture3 = CompletableFuture.supplyAsync(() -> this.getTotalTrNum(cold1DispatchReq.getStartTime(), cold1DispatchReq.getEndTime()), executor);


        //等待
        CompletableFuture.allOf(completableFuture1, completableFuture2, completableFuture3).join();

        //组数据
        Cold1Dispatch cold1Dispatch;
        try {
            cold1Dispatch = completableFuture1.get();
            Integer hqNum = completableFuture2.get();
            Integer totalTrNum = completableFuture3.get();
            if (cold1Dispatch == null) {
                cold1Dispatch = new Cold1Dispatch();
            }
            cold1Dispatch.setHqNum(hqNum);
            cold1Dispatch.setTotalTrNum(totalTrNum);
        } catch (Exception e) {
            log.error("获取冷端1调度数据失败：{}", e.getMessage());
            throw new ServiceException(ProduceErrorCode.GET_COLD1_DISPATCH_DATA_FAIL.getCode(), ProduceErrorCode.GET_COLD1_DISPATCH_DATA_FAIL.getMsg());
        }

        return cold1Dispatch;
    }

    @Override
    public Cold1Dispatch executeTask(Cold1DispatchReq cold1DispatchReq) {
        //计数统计
        Cold1Dispatch cold1Dispatch = baseMapper.getData(cold1DispatchReq);
        //统计横切数据
        int hqNum = this.getHqNum(cold1DispatchReq.getStartTime(), cold1DispatchReq.getEndTime());
        cold1Dispatch.setHqNum(hqNum);
        //冷端总投入数
        int totalTrNum = this.getTotalTrNum(cold1DispatchReq.getStartTime(), cold1DispatchReq.getEndTime());
        cold1Dispatch.setTotalTrNum(totalTrNum);
        return cold1Dispatch;
    }


    public int getHqNum(LocalDateTime startTime, LocalDateTime endTime) {
        long endTs = endTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long startTs = startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return baseMapper.getHqNum(String.valueOf(startTs), String.valueOf(endTs));
    }

    public int getTotalTrNum(LocalDateTime startTime, LocalDateTime endTime) {
        long endTs = endTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long startTs = startTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return baseMapper.getTotalTrNum(String.valueOf(startTs), String.valueOf(endTs));
    }

}
